<?php

namespace app\admin\controller;

class Npc extends Base
{
    protected function _infoModule()
    {
        return array(
            'info' => array(
                'name' => 'npc管理',
                'description' => 'npc管理',
            ),
            'menu' => array(
                array(
                    'name' => '列表',
                    'url' => url('index'),
                    'icon' => 'list',
                ),
            ),
            '_info' => array(
                array(
                    'name' => '添加',
                    'url' => url('info'),
                ),
            ),
        );
    }

    public function index()
    {
        $request = $this->request;

        //筛选条件
        $where = [];
        $pageMaps = [];

        $id = $request->post('id');
        $nname = $request->post('nname');
        $nickname = $request->post('nickname');

        if ($id) {
            $where['id'] = $id;
        }
        if ($nname) {
            $where['nname'] = ['like', '%' . $nname . '%'];
        }
        if ($nickname) {
            $where['nickname'] = ['like', '%' . $nickname . '%'];
        }

        //URL参数
        $pageMaps['id'] = $id;
        $pageMaps['nname'] = $nname;
        $pageMaps['nickname'] = $nickname;

        $pageAry = [
            'list_rows' => 15,
            'page' => input('page', 1),
            'path' => 'index',
            'query' => $pageMaps,
        ];

        //查询数据
        $list = model('Npc')->loadList($where, $pageAry,'nlv asc,id desc');
        if (!empty($list)) {
            foreach ($list as $key => $val) {
                $taskidAry = ($val['taskid'] != '' && $val['taskid'] != null) ? explode(',', $val['taskid']) : [];
                $taskList = [];
                if (!empty($taskidAry)) {
                    foreach ($taskidAry as $k => $item) {
                        $infoTask = model('Task')->getInfo($item);
                        $taskList[$k]['id'] = $infoTask['rwid'];
                        $taskList[$k]['name'] = $infoTask['rwname'];
                        $taskList[$k]['rwlx'] = $infoTask['rwlx'] == 1 ? "普通" : ($infoTask['rwlx'] == 2 ? '每日' : '主线');
                        $taskList[$k]['rwzl'] = $infoTask['rwzl'] == 1 ? "收集" : ($infoTask['rwzl'] == 2 ? '打怪' : '对话');
                        $taskList[$k]['rwlv'] = $infoTask['rwlv'];
                    }
                }
                $val['task_list'] = $taskList;
            }
        }
        $this->assign('list', $list);
        $this->assign('_page', $list->render());
        $this->assign('pageMaps', $pageMaps);

        return $this->fetch('index');
    }

    public function info()
    {
        $request = request();
        $id = input('id');
        $userNpc = model('Npc');
        if ($request->method() == 'POST') {
            $taskid = '';
            if (!empty($_POST['taskid'])){
                //预处理操作方法数据
                $taskid = $this->getAryTask($_POST['taskid']);
            }
            $_POST['taskid'] = $taskid;

            if ($id) {
                $status = $userNpc->editAll($request);
            } else {
                $status = $userNpc->addall($request);
            }
            if ($status !== false) {
                returnJson(1, '操作成功', ['url' => url('index')]);
            } else {
                returnJson(0, '操作失败');
            }
        } else {
            $info = $userNpc->getInfo($id);
            if (!empty($info)) {
                $info['task_list'] = [];
                if ($info['taskid'] != '') {
                    $ary = [];
                    $mnpcAry = explode(',', $info['taskid']);
                    foreach ($mnpcAry as $k => $item) {
                        $infoTask = model('Task')->getInfo($item);
                        $rwlx = $infoTask['rwlx'] == 1 ? "普通" : ($infoTask['rwlx'] == 2 ? '每日' : '主线');
                        $rwzl = $infoTask['rwzl'] == 1 ? "收集" : ($infoTask['rwzl'] == 2 ? '打怪' : '对话');

                        $rwinfo = substr_num($infoTask['rwinfo'], 0, 10);

                        $ary[$k]['id'] = $item;
                        $ary[$k]['name'] = '[' . $rwlx . '][' . $rwzl . '][lv:' . $infoTask['rwlv'] . ']-' . $infoTask['rwname'] . '<' . $rwinfo . '>';
                    }
                    $info['task_list'] = $ary;
                }
            }
            $this->assign('info', $info);
            //echo "<pre>";print_r($_POST);die;

            $num_task = empty($info) ? 1 : count($info['task_list']) + 1;
            $this->assign('num_task', $num_task);

            $this->fetch('info');
        }
    }

    protected function getAryTask($data)
    {
        $ary = [];
        if(!empty($data)){
            foreach($data as $key=>$vo){
                $ary[$key] = $vo['id'];
            }
        }
        return implode(',',$ary);
    }
    public function getTaskList()
    {
        $list = model('Task')->getList();
        if (!empty($list)) {
            foreach ($list as $k => $item) {
                $item['rwlx'] = $item['rwlx'] == 1 ? "普通" : ($item['rwlx'] == 2 ? '每日' : '主线');
                $item['rwzl'] = $item['rwzl'] == 1 ? "收集" : ($item['rwzl'] == 2 ? '打怪' : '对话');
            }
        }
        $this->assign('list', $list);
        return $this->fetch('task');
    }

    /**
     * 删除
     */
    public function del()
    {
        $request = $this->request;
        $id = $request->post('id');
        if (empty($id)) {
            returnJson(0, '参数不能为空');
        }

        if (model('Orders')->del($id)) {
            returnJson(1, '删除成功！');
        } else {
            returnJson(0, '删除失败');
        }
    }
}

